﻿function Save(DBNIndex) {
    DBNo = document.getElementById("divDBNo").value;
    fDate = document.getElementById("divDate").value;
    Payer = document.getElementById("divPayer").value;
    Subject = document.getElementById("divSubject").value;
    Attention = document.getElementById("divAttention").value;

    CurrencyIndex = document.getElementById("divCurrency").selectedIndex;
    Currency = document.getElementById("divCurrency").options[CurrencyIndex].value;

    Signer = document.getElementById("divSigner").value;
    Title = document.getElementById("divTitle").value;
    Alias = document.getElementById("divAlias").value;

    EnglishAccount = document.getElementById("divAccountEnglish").value;
    VietnameseAccount = document.getElementById("divAccountVietnamese").value;
    thueVat = document.getElementById("divPercentVat").value;

    ShortName = document.getElementById("txtShortCustomerName").value;
    CustomerName = document.getElementById("txtCustomerName").value;
    Address = document.getElementById("txtCustomerAddress").value;
    Phone = document.getElementById("txtCustomerPhone").value;

    StatusIndex = document.getElementById("divPaymentStatus").selectedIndex;

    pType = document.getElementById("drpSelectPType").options[document.getElementById("drpSelectPType").selectedIndex].value;
    fromLocationId = document.getElementById("selectFromDepartment").options[document.getElementById("selectFromDepartment").selectedIndex].value;
    toLocationId = document.getElementById("selectToDepartment").options[document.getElementById("selectToDepartment").selectedIndex].value;

    Max = document.getElementById("divMax").value;
    SaveItemData = "";
    for (var iIndex = 0; iIndex < Max; iIndex++) {
        Description = document.getElementById("divDescription_" + iIndex).value;
        Fee = document.getElementById("divFee_" + iIndex).value;
        NOIP = document.getElementById("divNOIP_" + iIndex).value;
        Postage = document.getElementById("divPostage_" + iIndex).value;
        PercentDiscount = document.getElementById("divPercentDiscount_" + iIndex).value;
        InternalSharing = document.getElementById("divInternalSharing_" + iIndex).value;
        Index = document.getElementById("divIndex_" + iIndex).value;

        SaveItemData = SaveItemData + Description;
        SaveItemData = SaveItemData + "~" + Fee;
        SaveItemData = SaveItemData + "~" + NOIP;
        SaveItemData = SaveItemData + "~" + Postage;
        SaveItemData = SaveItemData + "~" + PercentDiscount;
        SaveItemData = SaveItemData + "~" + Index;
        SaveItemData = SaveItemData + "~" + InternalSharing;
        if (iIndex < Max - 1) {
            SaveItemData = SaveItemData + "|";
        }
    }


    RetValue = Portal.Vision.UpdateQuotation.ServerSideSaveDebitNote(DBNIndex, Alias, DBNo, fDate, Payer, Subject, Attention, Currency, Signer, Title, EnglishAccount, VietnameseAccount, thueVat, StatusIndex, ShortName, CustomerName, Address, Phone, pType, fromLocationId, toLocationId, SaveItemData).value;
    if (RetValue != null) {
        if (RetValue.indexOf("ERROR") != -1) {
            alert(RetValue);
            return;
        }
    }

    alert("Cập nhật chào giá thành công!");
    Url = Portal.Vision.UpdateQuotation.ServerSideGetViewUrl(DBNIndex).value;
    window.open(Url, "_self");
}

function DeleteItem(Item) {
    if (confirm("Bạn đã chắc chắn xóa chưa?") == false) return;
    RetValue = Portal.Vision.UpdateQuotation.ServerSideDelDebitNoteItem(Item).value;
    if (RetValue != null) {
        if (RetValue.indexOf("ERROR") != -1) {
            alert(RetValue);
            return;
        }
    }
    alert("Xóa thành công. Nhấn Ok và F5 để đọc lại danh sách!");
}

function DoGetAccount() {
    if (confirm("Are you sure?") == false) return;
    Alias = document.getElementById("divAlias").value;
    document.getElementById("divAccountEnglish").value = Portal.Vision.UpdateQuotation.ServerSideGetAccountEnglish(Alias).value;
    document.getElementById("divAccountVietnamese").value = Portal.Vision.UpdateQuotation.ServerSideGetAccountVietnamese(Alias).value;
}



function ShowChangeAttention() {
    OldText = document.getElementById("divAttention").value;
    CaseType = document.getElementById("divCaseType").value;
    CaseIndex = document.getElementById("divCaseIndex").value;
    document.getElementById("divContentAttention").innerHTML = Portal.Vision.UpdateQuotation.ServerSideShowChangeAttention(CaseType, CaseIndex, OldText).value;
}

function ChangeAttention() {
    OldText = document.getElementById("divAttention").value;
    document.getElementById("divContentAttention").innerHTML = Portal.Vision.UpdateQuotation.ServerSideShowOldAttention(OldText).value;
}

function CancelChangeAttention() {
    OldText = document.getElementById("divOldAttention").value;
    document.getElementById("divContentAttention").innerHTML = Portal.Vision.UpdateQuotation.ServerSideShowOldAttention(OldText).value;
}


function DetectSignerTitle() {
    Signer = document.getElementById("divSigner").value;
    RetValue = Portal.Vision.UpdateQuotation.ServerSideDetectSignerTitle(Signer).value;
    if (RetValue.indexOf("ERROR") != -1) {
        alert(RetValue);
        return;
    }
    document.getElementById("divTitle").value = RetValue;
}


function DetectOriginCustomer() {
    CustIndex = document.getElementById("divCustIndex").value;
    ShortName = Portal.Vision.UpdateQuotation.ServerGetCustomerInfo(CustIndex, "ShortName").value;
    if (ShortName.indexOf("ERROR") != -1) {
        alert(ShortName);
        return;
    }

    document.getElementById("txtShortCustomerName").value = ShortName;

    CustName = Portal.Vision.UpdateQuotation.ServerGetCustomerInfo(CustIndex, "CustName").value;
    if (CustName.indexOf("ERROR") != -1) {
        alert(CustName);
        return;
    }

    document.getElementById("txtCustomerName").value = CustName;

    Address = Portal.Vision.UpdateQuotation.ServerGetCustomerInfo(CustIndex, "Address").value;
    if (Address.indexOf("ERROR") != -1) {
        alert(Address);
        return;
    }
    document.getElementById("txtCustomerAddress").value = Address;


    Phone = Portal.Vision.UpdateQuotation.ServerGetCustomerInfo(CustIndex, "Phone").value;
    if (Phone.indexOf("ERROR") != -1) {
        alert(Address);
        return;
    }
    document.getElementById("txtCustomerPhone").value = Phone;
}